# -*- coding: utf-8 -*-
import scrapy
from spider_CSDNCourse.items import CourseItem
from scrapy_redis.spiders import RedisSpider


class CourseSpiderSpider(RedisSpider):
    name = 'course_spider'
    # allowed_domains = ['edu.csdn.net']
    # start_urls = ['https://edu.csdn.net/course/detail/9182']
    redis_key="coursespider:start_urls"

    def __init__(self, *args, **kwargs):
        domain = kwargs.pop('domain', '')
        self.allowed_domains = filter(None, domain.split(','))
        super(CourseSpiderSpider, self).__init__(*args, **kwargs)

    def parse(self, response):
        '''解析课程详情'''
        page=""
        pages=response.css(".course_detail_page")
        if pages:
            page=pages[0]
            item=CourseItem()
            item["title"]=""
            item["hours"]=""
            item["teacher"]=""
            item["crowd"]=""
            item["stuNum"]=""
            item["price"]=""
            item["outline"]=""
            # 课程标题
            titles=page.css(".info_right h1::text")
            if titles:
                item["title"]=titles.extract_first()
            # 讲师
            teachers=page.css(".professor_name a::text")
            if teachers:
                item["teacher"]=teachers.extract_first()
            # 适合人群
            # 学习人数
            stunums=page.re("<span>([0-9]+)人已学习</span>")
            if stunums:
                item["stuNum"]=stunums[0]
            # 价格
            prices=page.re('<sapn class="money">¥([0-9\.]+)</sapn>')
            if prices:
                item["price"]=prices[0]
            # 课程大纲
            outline=[]
            hours=0 # 课时数
            temp_outlines=page.css(".outline_list") # 课程章节
            if temp_outlines:
                for line in temp_outlines:
                    # 章节标题
                    t=""
                    ts=line.css("dt span::text")
                    if ts:
                        t=ts.extract_first().strip("\n").strip(" ")
                    # 课程小节
                    lessons=[]
                    lessonlist=line.css("dd .section_list li")
                    for ls in lessonlist:
                        tlesson=ls.css(".section_name a::attr(title)")
                        if tlesson:
                            hours+=1
                            lessons.append(tlesson.extract_first())
                    chapterOBJ={"chapter":t,"lessons":lessons}
                    outline.append(chapterOBJ)
            item["outline"]=outline
            # 课时
            item["hours"]=hours
            print("课程信息：",item)
            yield item